package com.example.task1.ui.user.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.LoginBean;
import com.example.task1.util.ApiUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {

    private EditText userName;
    private EditText userPwd;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setTitle("登录");
        initView();
        btnLogin.setOnClickListener(v -> {
            String name = userName.getText().toString();
            String pwd = userPwd.getText().toString();
            if (TextUtils.isEmpty(name)) {
                userName.setError("账号不能为空");
            }else if (TextUtils.isEmpty(pwd)){
                userPwd.setError("密码不能为空");
            }else {
                HashMap<String, Object> map = new HashMap<>();
                map.put("username",name);
                map.put("password",pwd);
                login(map);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void login(HashMap<String, Object> map) {
        Call<ResponseBody> post = MyApplication.getApiService().post(" /prod-api/api/login", ApiUtils.getBody(map));
        post.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String body = response.body().string();
                    Gson gson = new Gson();
                    LoginBean loginBean = gson.fromJson(body, LoginBean.class);
                    if (loginBean.getCode() == 200) {
                        MyApplication.TOKEN = loginBean.getToken();
                        SharedPreferences.Editor edit = MyApplication.getSharedPreferences().edit();
                        edit.putString("token",loginBean.getToken());
                        MyApplication.getUserInfo(MyApplication.TOKEN);
                        edit.commit();
                        finish();
                    }else {
                        MyApplication.show("账号或密码错误");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }

    private void initView() {
        userName = (EditText) findViewById(R.id.user_name);
        userPwd = (EditText) findViewById(R.id.user_pwd);
        btnLogin = (Button) findViewById(R.id.btn_login);
    }
}